<template>
	<view class="body">
		<view class="head">
			<view class="head_left" @tap="back()">
				<image src="../../../static/wode/return.png" mode=""></image>
			</view>
			<view class="head_mid">
				设置密码
			</view>
			<view class="head_left">

			</view>
		</view>
		<view class="mima-box">
			<view class="mima-item padding-box" v-if="setPass==false">
				<image src="/static/index/mima.png" mode=""></image>
				<input type="password" value="" placeholder="输入原密码" v-model="oldPass2" @blur="inspectPass" />
				<image class="delete" src="/static/index/delete.png" mode="" @tap="deletenum('oldPass2')"></image>
			</view>
			<view class="padding-box" v-else>

			</view>
			<view class="mima-item">
				<image src="/static/index/mima.png" mode=""></image>
				<input type="password" value="" v-model="newPass" placeholder="输入新密码(密码由6-20为数字,字母组成)" />
				<image class="delete" src="/static/index/delete.png" mode="" @tap="deletenum('newPass')"></image>
			</view>
			<view class="mima-item">
				<image src="/static/index/mima.png" mode=""></image>
				<input type="password" value="" v-model="newPass2" placeholder="重复输入新密码" @blur="inspectNewPass" />
				<image class="delete" src="/static/index/delete.png" mode="" @tap="deletenum('newPass2')"></image>
			</view>
		</view>

		<view class="btn" @tap="tosetPass">
			确认
		</view>
		<view class="forget" @tap="toforgetPass" v-if="setPass==false">
			忘记密码
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				oldPass: '',
				oldPass2: '',
				newPass: '',
				newPass2: '',
				login_name: '',
				setPass: false,
			};
		},
		onLoad(option) {
			if (option.setPass != undefined) {
				this.setPass = option.setPass
			}
			if (option.loginName != undefined) {
				this.login_name = option.loginName
			}
		},
		onShow() {
			this.getPass()
		},
		methods: {
			toforgetPass() {
				uni.navigateTo({
					url: '/pages/My/forgetPass/forgetPass'
				})
			},
			getPass() {
				var me = this;
				var data = {
					user_id: uni.getStorageSync('user_id')
				}
				this.myAjaxNew({
					model: 'user',
					controllerName: 'user',
					actionName: 'query',
					data: data,
					successBack: function(res) {
						console.log(res)
						me.oldPass = res.data.data[0].login_pass
						me.login_name = res.data.data[0].login_name
						console.log(me.oldPass)
					}
				})
			},
			inspectPass() {
				if (this.oldPass != this.oldPass2) {
					uni.showToast({
						title: '原密码输入错误',
						icon: 'none'
					})
				}
			},
			inspectNewPass() {
				if (this.newPass != this.newPass2) {
					uni.showToast({
						title: '两次密码输入不一致',
						icon: 'none'
					})
				}
				console.log(this.newPass)
				console.log(this.newPass2)
			},
			deletenum(type) {
				if (type == 'oldPass2') {
					this.oldPass2 = ''
				} else if (type == 'newPass2') {
					this.newPass2 = ''
				} else {
					this.newPass = ''
				}
			},
			tosetPass() {
				if (this.setPass == false) {
					if (this.oldPass2 == '') {
						uni.showToast({
							title: '请输入原密码',
							icon: 'none'
						})
						return
					}
					if (this.newPass == '') {
						uni.showToast({
							title: '请输入新密码',
							icon: 'none'
						})
						return
					}
					if (this.newPass2 == '') {
						uni.showToast({
							title: '请再次输入新密码',
							icon: 'none'
						})
						return
					}
					if (this.newPass != this.newPass2) {
						uni.showToast({
							title: '两次密码输入不一致',
							icon: 'none'
						})
					}

					if (this.oldPass != this.oldPass2) {
						uni.showToast({
							title: '原密码输入错误',
							icon: 'none'
						})
						return
					}
					var me = this;
					var data = {
						login_pass: this.newPass,
						user_id: uni.getStorageSync('user_id'),
						login_name: this.login_name
					}
					this.myAjaxNewPost({
						model: 'user',
						controllerName: 'user',
						actionName: 'update',
						data: data,
						successBack: function(res) {
							console.log(res.data)
							if (res.data.message == '成功') {

								uni.navigateBack({
									delta: 3
								})
								uni.showToast({
									title: '密码修改成功',
									icon: 'none'
								})
							}
						},
					})
				} else {
					if (this.newPass == '') {
						uni.showToast({
							title: '请输入新密码',
							icon: 'none'
						})
						return
					}
					if (this.newPass2 == '') {
						uni.showToast({
							title: '请再次输入新密码',
							icon: 'none'
						})
						return
					}
					if (this.newPass != this.newPass2) {
						uni.showToast({
							title: '两次密码输入不一致',
							icon: 'none'
						})
					}
					var me = this;
					var data = {
						login_pass: this.newPass,
						user_id: uni.getStorageSync('user_id'),
						login_name: this.login_name
					}
					this.myAjaxNewPost({
						model: 'user',
						controllerName: 'user',
						actionName: 'update',
						data: data,
						successBack: function(res) {
							console.log(res.data)
							if (res.data.message == '成功') {
								if (this.data.user_id == null || this.data.user_id == '') {
									uni.navigateTo({
										url: '/pages/Other/login/login'
									})
								} else {
									uni.navigateBack({
										delta: 3
									})
								}

								uni.showToast({
									title: '密码修改成功',
									icon: 'none'
								})
							}
						},
					})
				}

			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},

		}

	}
</script>

<style scoped>
	.head {
		width: 100%;
		display: flex;
		justify-content: space-between;
		height: 162upx;
		background: #ffffff;
		padding: 0 24upx;
		z-index: 200;
		position: fixed;
		top: 0;
		box-sizing: border-box;
	}

	.head image {
		width: 20upx;
		height: 36upx;
		margin-right: 9upx;
	}

	.head_left {
		height: 82upx;
		width: 106upx;
		display: flex;
		align-items: center;
		line-height: 82upx;
		color: #666666;
		font-size: 32upx;
		padding-top: 70upx;
	}
	.head_mid{
		padding-top: 90upx;
	}

	.head_right {
		width: 150upx;
	}

	.body {
		height: 100%;
		background: #FFFFFF;
	}

	.mima-box {
		font-size: 28upx;
		padding: 0 24upx;
		padding-top: 162upx;
	}

	.mima-box image {
		width: 46upx;
		height: 40upx;
		margin-right: 20upx;
	}

	.mima-box input {
		width: 85%;
	}

	.mima-item {
		display: flex;
		height: 85upx;
		align-items: center;

		border-bottom: 1upx solid #E6E6E6;
	}

	.mima-box .delete {
		width: 25upx;
		height: 25upx;
	}

	.btn {
		margin-top: 88upx;
	}

	.forget {
		color: #666666;
		font-size: 28upx;
		position: absolute;
		right: 24upx;
		top: 650upx;
	}

	.padding-box {
		padding-top: 30upx;
	}
</style>
